#include "formfilesmanager.h"
#include "ui_formfilesmanager.h"

#include <QDebug>
#include <QMessageBox>
#include <global.h>

FormFilesManager::FormFilesManager(QWidget *parent) :
        QWidget(parent),
        ui(new Ui::FormFilesManager)
{
    struct masterParam param{};

    ui->setupUi(this);
    setWindowFlags(windowFlags() | Qt::Dialog);
    setWindowModality(Qt::ApplicationModal);
    setAttribute(Qt::WA_DeleteOnClose);

    mFileCopy = new FileManageThread();
    connect(mFileCopy, &FileManageThread::operateStationSig, this, &FormFilesManager::onProgressUpdate);

    mDbopt = new DCSDbOperator();
    if (DCSDbOperator::getMasterParameter(param))
    {
        ui->comboBoxMainSrc->setCurrentIndex(param.mainVideoSrc);
        ui->comboBoxBakSrc->setCurrentIndex(param.backupVideoSrc);
    }

    connect(ui->pushButtonReturn, &QPushButton::clicked, this, &FormFilesManager::onPushButtonReturnClicked);
    connect(ui->pushButtonCopy, &QPushButton::clicked, this, &FormFilesManager::onPushButtonCopyClicked);
    connect(ui->pushButtonClean, &QPushButton::clicked, this, &FormFilesManager::onPushButtonCleanClicked);
    connect(ui->comboBoxMainSrc, static_cast<void (QComboBox::*)(int)>(&QComboBox::currentIndexChanged), this,
            &FormFilesManager::onComboBoxMainSrcCurrentIndexChanged);
    connect(ui->comboBoxBakSrc, static_cast<void (QComboBox::*)(int)>(&QComboBox::currentIndexChanged), this,
            &FormFilesManager::onComboBoxBakSrcCurrentIndexChanged);

    qDebug() << "[FormFilesManager] \t Constructor";
}

FormFilesManager::~FormFilesManager()
{
    delete mFileCopy;
    delete mDbopt;
    delete ui;

    qDebug() << "[FormFilesManager] \t Destructor";
}

void FormFilesManager::onPushButtonReturnClicked()
{
    this->close();
}

void FormFilesManager::onComboBoxMainSrcCurrentIndexChanged(int index)
{
    DCSDbOperator::setMasterParameterMainVideoSrc(index);
}

void FormFilesManager::onComboBoxBakSrcCurrentIndexChanged(int index)
{
    DCSDbOperator::setMasterParameterBackupVideoSrc(index);
}

void FormFilesManager::onPushButtonCopyClicked()
{
    QString srcPath;

    ui->progressBar->setValue(0);

    if (0 == ui->comboBox->currentIndex())
    {
        srcPath.append(UDISK_PATH);
    }
    else if (1 == ui->comboBox->currentIndex())
    {
        srcPath.append(SDCARD_PATH);
    }

    mFileCopy->setSrcDirPath(srcPath);
    mFileCopy->setDesDirPath(EMMC_PATH);
    mFileCopy->operateStart(FILE_ACTION_COPY);
}

void FormFilesManager::onPushButtonCleanClicked()
{
    int ret;

    ret = QMessageBox::question(this, tr("内部存储"), tr("是否清空内部存储"), QMessageBox::Yes | QMessageBox::No, QMessageBox::No);
    if (ret == QMessageBox::Yes)
    {
        mFileCopy->setSrcDirPath(EMMC_PATH);
        mFileCopy->operateStart(FILE_ACTION_DEL);
    }
}

void FormFilesManager::onProgressUpdate(int flag, const QString &txt)
{
    quint8 progress;
    if (flag == OPERATE_COPY_TOTAL_RATE || flag == OPERATE_DELETE_TOTAL_RATE)
    {
        qDebug() << txt;
        progress = txt.toUInt();
        ui->progressBar->setValue(progress);

        if (progress == 100)
        {
            if (flag == OPERATE_COPY_TOTAL_RATE)
            {
                QMessageBox::information(this, tr("提示"), tr("拷贝完成"), QMessageBox::Yes);
            }
            else
            {
                QMessageBox::information(this, tr("提示"), tr("删除完成"), QMessageBox::Yes);
            }
            ui->progressBar->setValue(0);
        }
    }
    else if (flag == OPERATE_ERROR_MEM_FULL)
    {
        QMessageBox::warning(this, tr("提示"), tr("存储空间已满！"), QMessageBox::Yes);
    }
}
